﻿<?php
class Vfriend_Function {
	function isCached(&$result = null, $intDataType = 0) {
		if (!$result) return 0;
		if ($result == VFRIEND_NOCACHE) {
			$result = null;
		}
		return 1;
	}

	function redirect($strUrl) {
		header('location: ' . $strUrl);
	}
	function convertDateSys($intDay, $intMonth, $intYear) {
		return $intYear . '-' . $intMonth . '-' . $intDay;
	}
	function getYearByAge() {
		$intMin 	= intval(date('Y')) - USER_AGE_TO;
		$intMax		= intval(date('Y')) - USER_AGE_FROM;		
		for ($i = $intMax; $i >= $intMin; $i--) {
			$v = $i;
			$arrResult[$v] = $v;
		}
		return $arrResult;		
	}
	function getDayInMonth($intYear, $intMonth) {		
		$intMonth 	= intval($intMonth);
		switch($intMonth) {
			case 2:
				$intYear = intval($intMonth);
				if($intYear%4==0)
					return Vfriend_Function::arrayNumber(1,29,1);
				else
					return Vfriend_Function::arrayNumber(1,28,1);
				break;
			case 1:
			case 3:
			case 5:
			case 7:
			case 8:
			case 10:
			case 12:
				return Vfriend_Function::arrayNumber(1,31,1);
				break;
			case 4:
			case 6:
			case 9:
			case 11:
				return Vfriend_Function::arrayNumber(1,30,1);
				break;
			default:
				break;
		}
	}
	function arrayNumber($intMin = 0, $intMax = 0, $isEqualLength = 0)
	{
		for ($i = $intMin; $i <= $intMax; $i++) {
			$v = $i;
			if( $isEqualLength == 1 ) $v = substr(str_pad($i, strlen($intMax), '0', STR_PAD_LEFT), strlen($intMax) * -1);
			$arrResult[$v] = intval($v);
		}
		return $arrResult;
	}
	function gender($intGender=0) {
		$arrGender = array(1=>'Nam',2=>'Nữ');
		if($intGender > 0) {
			return $arrGender[$intGender];
		}
		else
			return $arrGender;
	}
	function now() {
		return date('Y-m-d H:i:s');
	}
	function logDb($strStore)
	{
		try {
			$strFolder = ROOT_DIR . '/data/log/' . date('Y/m/d/');
			Vfriend_Function::mkDirectory($strFolder);
			$strFile   = $strFolder . 'db.txt';
			$strContent = @file_get_contents($strFile);
			$strContent = $strContent . Vfriend_Function::now() . ' - '  . $strStore . "\n::::\n";
			@file_put_contents($strFile, $strContent);
		} catch(Exception $e) {
			$strFolder = ROOT_DIR . '/data/log/' . date('Y/m/d/');
			$strFile   = $strFolder . 'db.txt';
			@unlink($strFile);
		}		
	}
	function logFile($strFile,$str)
	{
		$strFolder = ROOT_DIR . '/data/log/';
		Vfriend_Function::mkDirectory($strFolder);
		$strFile   = $strFolder . $strFile .'.txt';
		$strContent = @file_get_contents($strFile);
		$strContent = $strContent . Vfriend_Function::now() . ' - '  . $str . "\n::::\n";
		@file_put_contents($strFile, $strContent);
	}
	function urlPaging($intParams = 0)
	{
		$strUrl = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
		if ($intParams == 0) {
			$intChr	= '?';
			$intPos	= strpos($strUrl, '?page=');
		} else {
			$intChr	= '&';
			$intPos	= strpos($strUrl, '&page=');
		}
		if ($intPos != false) {
			$strUrl = substr($strUrl, 0, $intPos);
		}
		return $strUrl . $intChr;
	}
	function base64E($str, $option=1)
	{
		if($option==1) {
			return str_replace('/', 'vfr', base64_encode($str));
		} else {
			return base64_encode($str);
		}
	}

	function base64D($str,$option=1)
	{
		if($option == 1) {
			return base64_decode(str_replace('vfr', '/', $str));
		} else {
			return base64_decode($str);
		}
	}
	public function setMsg($strMsg)
	{
		$_SESSION['VFRIEND_MESSAGE'] = Vfriend_Function::base64E($strMsg);
	}
	public function getMsg()
	{		
		$strMsg = '';
		if (isset($_SESSION['VFRIEND_MESSAGE'])) {
			$strMsg = Vfriend_Function::base64D($_SESSION['VFRIEND_MESSAGE']);
			unset($_SESSION['VFRIEND_MESSAGE']);
		}
		return $strMsg;
	}
	public function setError($strMsg)
	{
		$_SESSION['VFRIEND_ERROR'] = Vfriend_Function::base64E($strMsg);
	}
	public function getError()
	{
		$strMsg = '';
		if (isset($_SESSION['VFRIEND_ERROR'])) {
			$strMsg = Vfriend_Function::base64D($_SESSION['VFRIEND_ERROR']);
			unset($_SESSION['VFRIEND_ERROR']);
		}
		return $strMsg;
	}
	public function setSecret($intId, $strName) {
		$strToken	= md5(uniqid(rand(),1));
		$strSession	= $strName . $intId;
		$_SESSION[$strSession] = $strToken;
		return $strToken;
	}
	public function getSecret($intId, $strName) {
		$strSession = $strName . $intId;
		return isset($_SESSION[$strSession]) ? $_SESSION[$strSession] : '';
	}
	public function deleteSecret($intId, $strName) {
		$strSession = $strName . $intId;
		if(isset($_SESSION[$strSession])) {
			unset($_SESSION[$strSession]);
		}
	}
	public function getCookie($strName)
	{
		return (isset($_COOKIE[$strName]) ? $_COOKIE[$strName] : '');
	}
	public function setCookie($strName, $value, $expire = 0, $domain = DOMAIN)
	{
		if ($expire == 0) {
			$expire = time() + 14*24*60*60;
		} else {
			$expire = time() + $expire;
		}
		return setcookie($strName, $value, $expire, '/', '.' . $domain);
	}

	public function deleteCookie($strName, $domain = DOMAIN)
	{
		//setcookie($strName, '', 1, '/', '.' . $domain);
		setcookie($strName, '', time()-3600);
	}
	static function mkDirectory($target) {
		// from php.net/mkdir user contributed notes
		if (file_exists ( $target )) {
			if (! @ is_dir ( $target ))
				return false;
			else
				return true;
		}
		
		// Attempting to create the directory may clutter up our display.
		if (@ mkdir ( $target )) {
			$stat = @ stat ( dirname ( $target ) );
			$dir_perms = $stat ['mode'] & 0007777; // Get the permission bits.
			@ chmod ( $target, $dir_perms );
			return true;
		} else {
			if (is_dir ( dirname ( $target ) ))
				return false;
		}
		
		// If the above failed, attempt to create the parent node, then try again.
		if (Vfriend_Function::mkDirectory ( dirname ( $target ) ))
			return mkDir ( $target );
		
		return false;
	}
	public function convertArrToStr($arr) {
		
		if(is_array($arr)) {
			$str = '';
			$length = count($arr);			
			for($i=0;$i<$length;$i++) {
				$str .= $arr[$i];
				if($length-$i > 1) {
					$str .=',';
				}
			}
			return $str;
		}
		return '';
	}
	public function convertArrToInt($arr) {
		if(is_array($arr)) {
			$int = '';
			$length = count($arr);
			for($i=0;$i<$length;$i++) {
				$int += $arr[$i];				
			}
			return $int;
		}
		return 0;
	}
	public function getQuote(&$str) {
		$arrMatch	= array();
		$strJson	= '';
		$str		= str_replace(array('<p>[',']</p>',"\n"),array('[',']','<br/>'),$str);		
		if(preg_match('/\[(quote|QUOTE)(.*)\](.*)\[\/(quote|QUOTE)\](.*)$/', $str, $arrMatch)) {
			$strId		= isset($arrMatch[2]) ? $arrMatch[2] : '';			
			$strQuote	= isset($arrMatch[3]) ? $arrMatch[3] : '';
			$str		= isset($arrMatch[5]) ? $arrMatch[5] : '';
			if($strQuote != '') {
				$arrId	= null;
				if($strId != '') $arrId	= explode(',',$strId);
				if(isset($arrId) && count($arrId) > 1) {
					$strJson = Zend_Json::encode(array('hexid'=>$arrId[0],'ncommentid'=>$arrId[1],'short_content'=>$strQuote));
				} else {
					$strJson = Zend_Json::encode(array('hexid'=>0,'ncommentid'=>0,'short_content'=>$strQuote));
				}				
			}			
		}
		return Vfriend_Function::stripTags($strJson,3);
	}
	public function isValidDate($str) {
		if($str == '') return false;
		$arrExp 	= explode('/', $str);
		$intDay		= isset($arrExp[0]) ? (int)$arrExp[0] : null;
		$intMonth	= isset($arrExp[1]) ? (int)$arrExp[1] : null;	
		$intYear	= isset($arrExp[2]) ? (int)$arrExp[2] : null;	
		return checkdate($intMonth, $intDay, $intYear);
	}
	public function isValidHour($intHour, $intMinute) {	
		if((int)$intHour < 24 && (int)$intMinute < 60) {				
			return true;
		}else {
			return false;
		}
	}
	public function isValidUsername($strUsername) {
		if(preg_match('/^(([A-Za-z\d][A-Za-z\d_]{4,29}[a-zA-Z\d])|([a-zA-Z\d]{5,30}))$/', $strUsername)) {
			return true;
		}
		return false;
	}
	public function isValidMobile($strMobile) {
		if(preg_match('/^((09[\d]{8})|(^01[\d]{9}))$/', $strMobile)) {
			return true;
		}
		return false;
	}
	public function isValidObject($strLink) {			
		if(preg_match('/^<object[^>]*?.*?<\/object>$/', $strLink)) {			
			return true;
		}
		return false;
	}
	public function isValidLink($strLink) {
		if($strLink == '') return false;
		$strWww		= 'www.' . DOMAIN;
		$arrDomain	= array(DOMAIN=>DOMAIN, $strWww=>$strWww);
		$strDomain 	= parse_url($strLink, PHP_URL_HOST);
		$strIp		= gethostbyname($strDomain);
		if(array_key_exists($strDomain, $arrDomain) || $strIp == '118.69.198.230' || $strIp == '127.0.0.1') {
			return true;
		}
		return false;		
	}
	public function isValidCode($strCode) {		
		if(preg_match('/^([a-zA-Z]{2,50})$/', $strCode)) {			
			return true;
		}
		return false;
	}
	public function getBrowser() {
		$u_agent 	= $_SERVER['HTTP_USER_AGENT'];
		$ub			= '';
		if(preg_match('/MSIE/i',$u_agent) && !preg_match('/Opera/i',$u_agent))
		{
			$ub = "MSIE";
		}
		elseif(preg_match('/Firefox/i',$u_agent))
		{
			$ub = "Firefox";
		}
		elseif(preg_match('/Chrome/i',$u_agent))
		{
			$ub = "Chrome";
		}
		elseif(preg_match('/Safari/i',$u_agent))
		{
			$ub = "Safari";
		}
		elseif(preg_match('/Opera/i',$u_agent))
		{
			$ub = "Opera";
		}
		elseif(preg_match('/Netscape/i',$u_agent))
		{
			$ub = "Netscape";
		} 
		return $ub;
	}
	public function convertStrToDate($strDate) {
		if($strDate == '') return '';
		$arrExplode = explode('/', $strDate);		
		if(count($arrExplode) > 1) {
			return $arrExplode[2] . '-' . $arrExplode[1] . '-' . $arrExplode[0];
		}
		return '';
	}
	public function setUserLogs($intAdminId, $intStatus) {				
		$data = array('adminid'=>$intAdminId,'statuses'=>$intStatus);
		$data = Zend_Json::encode($data);
		return $data;
	}
	public function getUserLogs($data) {				
		$data = Zend_Json::decode($data);
		return $data;
	}
	public function viewEmoticon() {
		$arrResult = array(
							':)'	=> array(
											'text'	=>	':)',
											'title'	=> 	'Vui',
											'image'	=>	HOST_IMG . 'emoticon/001.gif'
										),							
							':('	=> array(
											'text'	=>	':(',
											'title'	=> 	'Buồn',
											'image'	=>	HOST_IMG . 'emoticon/002.gif'
										),
							':D'	=> array(
											'text'	=>	':D',
											'title'	=> 	'Cười nhe răng',
											'image'	=>	HOST_IMG . 'emoticon/003.gif'
										),
							'8*'	=> array(
											'text'	=>	'8*',
											'title'	=> 	'Ngạc nhiên',
											'image'	=>	HOST_IMG . 'emoticon/004.gif'
										),
							'>('	=> array(
											'text'	=>	'>(',
											'title'	=> 	'Đa nghi',
											'image'	=>	HOST_IMG . 'emoticon/005.gif'
										),
							'[/'	=> array(
											'text'	=>	'[/',
											'title'	=> 	'Mặt ngầu',
											'image'	=>	HOST_IMG . 'emoticon/006.gif'
										),
							'@.@'	=> array(
											'text'	=>	'@.@',
											'title'	=> 	'Loạn óc',
											'image'	=>	HOST_IMG . 'emoticon/007.gif'
										),
							':(('	=> array(
											'text'	=>	':((',
											'title'	=> 	'Khóc lóc',
											'image'	=>	HOST_IMG . 'emoticon/008.gif'
										),
							'|:('	=> array(
											'text'	=>	'|:(',
											'title'	=> 	'Cẩn thận',
											'image'	=>	HOST_IMG . 'emoticon/009.gif'
										),
							'|D'	=> array(
											'text'	=>	'|D',
											'title'	=> 	'Nham nhở',
											'image'	=>	HOST_IMG . 'emoticon/010.gif'
										),
							'T_T'	=> array(
											'text'	=>	'T_T',
											'title'	=> 	'Hu hu',
											'image'	=>	HOST_IMG . 'emoticon/011.gif'
										),
							'[P'	=> array(
											'text'	=>	'[P',
											'title'	=> 	'Xí xóa nhé',
											'image'	=>	HOST_IMG . 'emoticon/012.gif'
										),
							'[S'	=> array(
											'text'	=>	'[S',
											'title'	=> 	'Lo lắng',
											'image'	=>	HOST_IMG . 'emoticon/013.gif'
										),
							']3'	=> array(
											'text'	=>	']3',
											'title'	=> 	'Giả nai',
											'image'	=>	HOST_IMG . 'emoticon/014.gif'
										),							
							']>'	=> array(
											'text'	=>	']>',
											'title'	=> 	'Ngại ngùng',
											'image'	=>	HOST_IMG . 'emoticon/015.gif'
										),
							'>:('	=> array(
											'text'	=>	'>:(',
											'title'	=> 	'Bực mình',
											'image'	=>	HOST_IMG . 'emoticon/016.gif'
										),
							'#)'	=> array(
											'text'	=>	'#)',
											'title'	=> 	'Yêu rồi',
											'image'	=>	HOST_IMG . 'emoticon/017.gif'
										),
							'#('	=> array(
											'text'	=>	'#(',
											'title'	=> 	'U trán',
											'image'	=>	HOST_IMG . 'emoticon/018.gif'
										),
							'[8)'	=> array(
											'text'	=>	'[8)',
											'title'	=> 	'Mặt gian',
											'image'	=>	HOST_IMG . 'emoticon/019.gif'
										),
							'-_-'	=> array(
											'text'	=>	'-_-',
											'title'	=> 	'Không vừa lòng',
											'image'	=>	HOST_IMG . 'emoticon/020.gif'
										),
							'#_#'	=> array(
											'text'	=>	'#_#',
											'title'	=> 	'Choáng',
											'image'	=>	HOST_IMG . 'emoticon/021.gif'
										),
							'\:D'	=> array(
											'text'	=>	'\:D',
											'title'	=> 	'Bề trên',
											'image'	=>	HOST_IMG . 'emoticon/022.gif'
										),
							'._.'	=> array(
											'text'	=>	'._.',
											'title'	=> 	'Mặt ngu',
											'image'	=>	HOST_IMG . 'emoticon/023.gif'
										),
							'P_P'	=> array(
											'text'	=>	'P_P',
											'title'	=> 	'Mắt long lanh',
											'image'	=>	HOST_IMG . 'emoticon/024.gif'
										),
							'>:#'	=> array(
											'text'	=>	'>:#',
											'title'	=> 	'Tức điên',
											'image'	=>	HOST_IMG . 'emoticon/025.gif'
										),
							'8x'	=> array(
											'text'	=>	'8X',
											'title'	=> 	'Bịt mồm',
											'image'	=>	HOST_IMG . 'emoticon/026.gif'
										),
							'#K'	=> array(
											'text'	=>	'#K',
											'title'	=> 	'Muốn xé xác',
											'image'	=>	HOST_IMG . 'emoticon/027.gif'
										),
		);
		return $arrResult;
	}
	public function replaceEmoticon() {
		$arrResult = array(
							# 3 chars
							':-)'	=> array(
											'text'	=>	':)',
											'title'	=> 	'Vui',
											'image'	=>	HOST_IMG . 'emoticon/001.gif'
										),
							':-('	=> array(
											'text'	=>	':-(',
											'title'	=> 	'Buồn',
											'image'	=>	HOST_IMG . 'emoticon/002.gif'
										),
							':-d'	=> array(
											'text'	=>	':-D',
											'title'	=> 	'Cười nhe răng',
											'image'	=>	HOST_IMG . 'emoticon/003.gif'
										),
							':-D'	=> array(
											'text'	=>	':-D',
											'title'	=> 	'Cười nhe răng',
											'image'	=>	HOST_IMG . 'emoticon/003.gif'
										),
							'@.@'	=> array(
											'text'	=>	'@.@',
											'title'	=> 	'Loạn óc',
											'image'	=>	HOST_IMG . 'emoticon/007.gif'
										),
							':(('	=> array(
											'text'	=>	':((',
											'title'	=> 	'Khóc lóc',
											'image'	=>	HOST_IMG . 'emoticon/008.gif'
										),
							'|:('	=> array(
											'text'	=>	'|:(',
											'title'	=> 	'Cẩn thận',
											'image'	=>	HOST_IMG . 'emoticon/009.gif'
										),
							'T_T'	=> array(
											'text'	=>	'T_T',
											'title'	=> 	'Hu hu',
											'image'	=>	HOST_IMG . 'emoticon/011.gif'
										),
							't_T'	=> array(
											'text'	=>	'T_T',
											'title'	=> 	'Hu hu',
											'image'	=>	HOST_IMG . 'emoticon/011.gif'
										),
							't_t'	=> array(
											'text'	=>	'T_T',
											'title'	=> 	'Hu hu',
											'image'	=>	HOST_IMG . 'emoticon/011.gif'
										),
							'T_t'	=> array(
											'text'	=>	'T_T',
											'title'	=> 	'Hu hu',
											'image'	=>	HOST_IMG . 'emoticon/011.gif'
										),
							'>:('	=> array(
											'text'	=>	'>:(',
											'title'	=> 	'Bực mình',
											'image'	=>	HOST_IMG . 'emoticon/016.gif'
										),
							'[8)'	=> array(
											'text'	=>	'[8)',
											'title'	=> 	'Mặt gian',
											'image'	=>	HOST_IMG . 'emoticon/019.gif'
										),
							'-_-'	=> array(
											'text'	=>	'-_-',
											'title'	=> 	'Không vừa lòng',
											'image'	=>	HOST_IMG . 'emoticon/020.gif'
										),
							'#_#'	=> array(
											'text'	=>	'#_#',
											'title'	=> 	'Choáng',
											'image'	=>	HOST_IMG . 'emoticon/021.gif'
										),
							'\:D'	=> array(
											'text'	=>	'\:D',
											'title'	=> 	'Bề trên',
											'image'	=>	HOST_IMG . 'emoticon/022.gif'
										),
							'\:d'	=> array(
											'text'	=>	'\:D',
											'title'	=> 	'Bề trên',
											'image'	=>	HOST_IMG . 'emoticon/022.gif'
										),
							'._.'	=> array(
											'text'	=>	'._.',
											'title'	=> 	'Mặt ngu',
											'image'	=>	HOST_IMG . 'emoticon/023.gif'
										),
							'P_P'	=> array(
											'text'	=>	'P_P',
											'title'	=> 	'Mắt long lanh',
											'image'	=>	HOST_IMG . 'emoticon/024.gif'
										),
							'p_P'	=> array(
											'text'	=>	'P_P',
											'title'	=> 	'Mắt long lanh',
											'image'	=>	HOST_IMG . 'emoticon/024.gif'
										),
							'p_p'	=> array(
											'text'	=>	'P_P',
											'title'	=> 	'Mắt long lanh',
											'image'	=>	HOST_IMG . 'emoticon/024.gif'
										),
							'P_p'	=> array(
											'text'	=>	'P_P',
											'title'	=> 	'Mắt long lanh',
											'image'	=>	HOST_IMG . 'emoticon/024.gif'
										),
							'>:#'	=> array(
											'text'	=>	'>:#',
											'title'	=> 	'Tức điên',
											'image'	=>	HOST_IMG . 'emoticon/025.gif'
										),
							# 2 chars	
							':)'	=> array(
											'text'	=>	':)',
											'title'	=> 	'Vui',
											'image'	=>	HOST_IMG . 'emoticon/001.gif'
										),
							
							':('	=> array(
											'text'	=>	':(',
											'title'	=> 	'Buồn',
											'image'	=>	HOST_IMG . 'emoticon/002.gif'
										),							
							':D'	=> array(
											'text'	=>	':D',
											'title'	=> 	'Cười nhe răng',
											'image'	=>	HOST_IMG . 'emoticon/003.gif'
										),							
							':d'	=> array(
											'text'	=>	':D',
											'title'	=> 	'Cười nhe răng',
											'image'	=>	HOST_IMG . 'emoticon/003.gif'
										),
							
							'8*'	=> array(
											'text'	=>	'8*',
											'title'	=> 	'Ngạc nhiên',
											'image'	=>	HOST_IMG . 'emoticon/004.gif'
										),
							'>('	=> array(
											'text'	=>	'>(',
											'title'	=> 	'Đa nghi',
											'image'	=>	HOST_IMG . 'emoticon/005.gif'
										),
							'[/'	=> array(
											'text'	=>	'[/',
											'title'	=> 	'Mặt ngầu',
											'image'	=>	HOST_IMG . 'emoticon/006.gif'
										),
							
							'|D'	=> array(
											'text'	=>	'|D',
											'title'	=> 	'Nham nhở',
											'image'	=>	HOST_IMG . 'emoticon/010.gif'
										),
							'|d'	=> array(
											'text'	=>	'|D',
											'title'	=> 	'Nham nhở',
											'image'	=>	HOST_IMG . 'emoticon/010.gif'
										),
							
							'[P'	=> array(
											'text'	=>	'[P',
											'title'	=> 	'Xí xóa nhé',
											'image'	=>	HOST_IMG . 'emoticon/012.gif'
										),
							'[p'	=> array(
											'text'	=>	'[P',
											'title'	=> 	'Xí xóa nhé',
											'image'	=>	HOST_IMG . 'emoticon/012.gif'
										),
							'[S'	=> array(
											'text'	=>	'[S',
											'title'	=> 	'Lo lắng',
											'image'	=>	HOST_IMG . 'emoticon/013.gif'
										),
							'[s'	=> array(
											'text'	=>	'[S',
											'title'	=> 	'Lo lắng',
											'image'	=>	HOST_IMG . 'emoticon/013.gif'
										),
							']3'	=> array(
											'text'	=>	']3',
											'title'	=> 	'Giả nai',
											'image'	=>	HOST_IMG . 'emoticon/014.gif'
										),							
							']>'	=> array(
											'text'	=>	']>',
											'title'	=> 	'Ngại ngùng',
											'image'	=>	HOST_IMG . 'emoticon/015.gif'
										),							
							'#)'	=> array(
											'text'	=>	'#)',
											'title'	=> 	'Yêu rồi',
											'image'	=>	HOST_IMG . 'emoticon/017.gif'
										),
							'#('	=> array(
											'text'	=>	'#(',
											'title'	=> 	'U trán',
											'image'	=>	HOST_IMG . 'emoticon/018.gif'
										),							
							'8x'	=> array(
											'text'	=>	'8X',
											'title'	=> 	'Bịt mồm',
											'image'	=>	HOST_IMG . 'emoticon/026.gif'
										),
							'#K'	=> array(
											'text'	=>	'#K',
											'title'	=> 	'Muốn xé xác',
											'image'	=>	HOST_IMG . 'emoticon/027.gif'
										),
							'#k'	=> array(
											'text'	=>	'#K',
											'title'	=> 	'Muốn xé xác',
											'image'	=>	HOST_IMG . 'emoticon/027.gif'
										),
		);
		return $arrResult;
	}
	function stripTags($strSrc, $intType=1)
	{
		$strSrc = str_replace(array('&lt;','&gt;','<br/><p>'), array('<', '>','<p>'), $strSrc);
		$strSrc = trim($strSrc);
		$strSrc	= html_entity_decode($strSrc, ENT_NOQUOTES, 'UTF-8');
		if($intType == 0) {
			$strAllowTags	= "\n";
		} elseif($intType == 1) { 
			$strAllowTags 	= '<p>,<font>,<span>,<a>,<img>,<h1>,<h2>,<h3>,<h4>,<h5>,<h6>,<strong>,<b>,<em>,<i>,<br/>,<u>,' . "\n";
		} elseif($intType == 2) { // cmt
			$strAllowTags 	= '<a>,<img>,<br/>,' . "\n";
		} elseif($intType == 3) { // cmt has quote
			$strAllowTags	= '<p>,<strong>,<b>,<em>,<i>,<u>,<span>,<img>,<br/>,' . "\n";
		} elseif($intType == 4) { // smart search
			$strAllowTags	= '<ul>,<li>,<a>,<strong>';
		} else { // create
			$strAllowTags 	= '<p>,<font>,<span>,<a>,<img>,<h1>,<h2>,<h3>,<h4>,<h5>,<h6>,<strong>,<b>,<em>,<i>,<br>,<br/>,<u>,' . "\n" . ',<sub>,<sub>,<video>,<iframe>,<object>,<param>,<embed>,<ul>,<ol>,<li>,<table>,<tbody>,<thead>,<tfoot>,<tr>,<th>,<td>';
		}
		$strSrc = strip_tags($strSrc, $strAllowTags);
		$strSrc = str_replace('</p><br/>','</p>',$strSrc);
		return $strSrc;
	}
	function getUrl() {
		$ref = $_SERVER['REQUEST_URI'];
		$arr = parse_url($ref);
		parse_str($arr['query'], $query);	
		if(isset($query['page']))
			unset($query['page']);
		$arr['query'] 	= http_build_query($query);
		$strQuery		= str_replace('&','&amp;',$arr['query']);
		return 'http://'.$_SERVER['SERVER_NAME'].$arr['path'].'?'.$strQuery . '&amp;';
	}
	function convertNoVn($string, $length = 100, $strSymbol='-', $isToLower = 1)
	{
		$arrCharFrom= array(
							   "ạ","á","à","ả","ã","Ạ","Á","À","Ả","Ã",
							   "â","ậ","ấ","ầ","ẩ","ẫ","Â","Ậ","Ấ","Ầ","Ẩ","Ẫ",
							   "ă","ặ","ắ","ằ","ẳ","ẫ","Ă","Ắ","Ằ","Ẳ","Ẵ","Ặ",
							   "ê","ẹ","é","è","ẻ","ẽ","Ê","Ẹ","É","È","Ẻ","Ẽ",
							   "ế","ề","ể","ễ","ệ","Ế","Ề","Ể","Ễ","Ệ",
							   "ọ","ộ","ổ","ỗ","ố","ồ","Ọ","Ộ","Ổ","Ỗ","Ố","Ồ","Ô","ô",
							   "ó","ò","ỏ","õ","Ó","Ò","Ỏ","Õ",
							   "ơ","ợ","ớ","ờ","ở","ỡ",
							   "Ơ","Ợ","Ớ","Ờ","Ở","Ỡ",
							   "ụ","ư","ứ","ừ","ử","ữ","ự","Ụ","Ư","Ứ","Ừ","Ử","Ữ","Ự",
							   "ú","ù","ủ","ũ","Ú","Ù","Ủ","Ũ",
							   "ị","í","ì","ỉ","ĩ","Ị","Í","Ì","Ỉ","Ĩ",
							   "ỵ","ý","ỳ","ỷ","ỹ","Ỵ","Ý","Ỳ","Ỷ","Ỹ",
							   "đ","Đ"
				   		);
	   $arrCharEndNoVn= array(
							   "a","a","a","a","a","A","A","A","A","A",
							   "a","a","a","a","a","a","A","A","A","A","A","A",
							   "a","a","a","a","a","a","A","A","A","A","A","A",
							   "e","e","e","e","e","e","E","E","E","E","E","E",
							   "e","e","e","e","e","E","E","E","E","E",
							   "o","o","o","o","o","o","O","O","O","O","O","O","O","o",
							   "o","o","o","o","O","O","O","O",
							   "o","o","o","o","o","o",
							   "O","O","O","O","O","O",
							   "u","u","u","u","u","u","u","U","U","U","U","U","U","U",
							   "u","u","u","u","U","U","U","U",
							   "i","i","i","i","i","I","I","I","I","I",
							   "y","y","y","y","y","Y","Y","Y","Y","Y",
							   "d","D"
	   						);
		$arrCharFilter = str_replace($arrCharFrom,$arrCharEndNoVn,trim($string));
		if($strSymbol != '')		{
			if (mb_strlen($arrCharFilter,"UTF-8") > $length) {
				$arrCharFilter = mb_substr($arrCharFilter, 0, $length,"UTF-8");
			}		
			$arrCharFilter = preg_replace('/[\W|_]+/',$strSymbol,$arrCharFilter);
		}
		return $isToLower ? strtolower($arrCharFilter) : $arrCharFilter;
	}
	public function UTF8Deaccent($str)
	{
    	$trans = array
    	(
			// C1 Controls and Latin-1 Supplement (0080 - 00FF)
			'À' => 'A', 'Á' => 'A', 'Â' => 'A', 'È' => 'E',
			'É' => 'E', 'Ê' => 'E', 'Ì' => 'I', 'Í' => 'I',
			'Ò' => 'O', 'Ó' => 'O', 'Ô' => 'O', 'Õ' => 'O',
			'Ù' => 'U', 'Ú' => 'U', 'à' => 'a', 'á' => 'a',
			'â' => 'a', 'ã' => 'a', 'è' => 'e', 'é' => 'e',
			'ê' => 'e', 'ì' => 'i', 'í' => 'i', 'ò' => 'o',
			'ó' => 'o', 'ô' => 'o', 'õ' => 'o', 'ù' => 'u',
			'ú' => 'u', 'ý' => 'y',
			// Latin Extended-A (0100 - 017F)
			'Ă' => 'A', 'ă' => 'a', 'Đ' => 'D', 'đ' => 'd',
			'Ĩ' => 'I', 'ĩ' => 'i',	'Ũ' => 'U', 'ũ' => 'u',
			// Latin Extended-B (0180 - 024F)
			'Ơ' => 'O', 'ơ' => 'o',	'Ư' => 'U', 'ư' => 'u',
			// Latin Extended Additional (1E80 - 1EFF)
			'Ạ' => 'A', 'ạ' => 'a',	'Ả' => 'A', 'ả' => 'a',
			'Ấ' => 'A', 'ấ' => 'a',	'Ầ' => 'A', 'ầ' => 'a',
			'Ẩ' => 'A', 'ẩ' => 'a',	'Ẫ' => 'A', 'ẫ' => 'a',
			'Ậ' => 'A', 'ậ' => 'a', 'Ắ' => 'A', 'ắ' => 'a',
			'Ằ' => 'A', 'ằ' => 'a', 'Ẳ' => 'A', 'ẳ' => 'a',
			'Ẵ' => 'A', 'ẵ' => 'a', 'Ặ' => 'A', 'ặ' => 'a',
			'Ẹ' => 'E', 'ẹ' => 'e', 'Ẻ' => 'E', 'ẻ' => 'e',
			'Ẽ' => 'E', 'ẽ' => 'e', 'Ế' => 'E', 'ế' => 'e',
			'Ề' => 'E', 'ề' => 'e', 'Ể' => 'E', 'ể' => 'e',
			'Ễ' => 'E', 'ễ' => 'e', 'Ệ' => 'E', 'ệ' => 'e',
			'Ỉ' => 'I', 'ỉ' => 'i', 'Ị' => 'I', 'ị' => 'i',
			'Ọ' => 'O', 'ọ' => 'o', 'Ỏ' => 'O', 'ỏ' => 'o',
			'Ố' => 'O', 'ố' => 'o', 'Ồ' => 'O', 'ồ' => 'o',
			'Ổ' => 'O', 'ổ' => 'o', 'Ỗ' => 'O', 'ỗ' => 'o',
			'Ộ' => 'O', 'ộ' => 'o', 'Ớ' => 'O', 'ớ' => 'o',
			'Ờ' => 'O', 'ờ' => 'o', 'Ở' => 'O', 'ở' => 'o',
			'Ỡ' => 'O', 'ỡ' => 'o', 'Ợ' => 'O', 'ợ' => 'o',
			'Ụ' => 'U', 'ụ' => 'u', 'Ủ' => 'U', 'ủ' => 'u',
			'Ứ' => 'U', 'ứ' => 'u', 'Ừ' => 'U', 'ừ' => 'u',
			'Ử' => 'U', 'ử' => 'u', 'Ữ' => 'U', 'ữ' => 'u',
			'Ự' => 'U', 'ự' => 'u', 'Ỳ' => 'Y', 'ỳ' => 'y',
			'Ỵ' => 'Y', 'ỵ' => 'y',	'Ỷ' => 'Y', 'ỷ' => 'y',
			'Ỹ' => 'Y', 'ỹ' => 'y'
		);
		return strtr($str, $trans);
	}
	function wordCut($sText, $iMaxLength=500, $sMessage='...',$option=0)
	{
		if($option == 0)
			$sText = html_entity_decode(Vfriend_Function::stripTags($sText,0), ENT_NOQUOTES, 'UTF-8');	
		elseif($option == 1) 
			$sText = html_entity_decode(Vfriend_Function::stripTags($sText,3), ENT_NOQUOTES, 'UTF-8');	
	    if (strlen($sText) > $iMaxLength)
	    {			
			$sString 			= wordwrap($sText, ($iMaxLength-strlen($sMessage)), '[cut]', 1);
			$asExplodedString 	= explode('[cut]', $sString);      
			$sCutText 			= $asExplodedString[0];      
			$sReturn 			= $sCutText . $sMessage;
	    }
	    else
	    {
		   $sReturn = $sText;
	    }	  
		return $sReturn;
	}
	public function numberFormat($number, $decimals="0", $decpoint=",", $thousandsep=".") {
		return number_format($number, $decimals, $decpoint, $thousandsep);
	}
	public function daysInMonth($month, $year) {
		if(checkdate($month, 31, $year)) return 31;
		if(checkdate($month, 30, $year)) return 30;
		if(checkdate($month, 29, $year)) return 29;
		if(checkdate($month, 28, $year)) return 28;
		return 0; // error
	} 	
	/** REWRITE LINK */
	public function linkFullHouse($strUserName, $intHexId, $strTitle)
	{
		if($strUserName != '') {
			return HOST_VFRIEND . $strUserName;
		} else {
			$strTitle 	= str_replace('/', ' ', $strTitle);
			return HOST_VFRIEND . $intHexId . '/' . Vfriend_Function::convertNoVn($strTitle) . '.html';
		}
	}
	public function linkFullhouseForUblast($strUsername, $intHexId, $intId) {				
		if($strUsername != '') {
			return HOST_VFRIEND . $strUsername . '/tam-su/ub' . $intId . '.html';
		} else {
			return HOST_VFRIEND . $intHexId . '/tam-su/ub' . $intId . '.html';
		}
	}
	public function linkFullhouseForInform($strUsername, $intHexId) {		
		if($strUsername != '') {
			return HOST_VFRIEND . $strUsername . '/thong-tin.html';
		} else {
			return HOST_VFRIEND . $intHexId . '/thong-tin.html';
		}
	}
	public function linkFullhouseForFriend($strUsername, $intHexId) {		
		if($strUsername != '') {
			return HOST_VFRIEND . $strUsername . '/ban-be.html';
		} else {
			return HOST_VFRIEND . $intHexId . '/ban-be.html';
		}
	}
	public function linkFullhouseForFortune($strUsername, $intHexId) {		
		if($strUsername != '') {
			return HOST_VFRIEND . $strUsername . '/tai-san.html';
		} else {
			return HOST_VFRIEND . $intHexId . '/tai-san.html';
		}
	}
	public function linkFullhouseForBehaviour($strUsername, $intHexId) {		
		if($strUsername != '') {
			return HOST_VFRIEND . $strUsername . '/hoat-dong.html';
		} else {
			return HOST_VFRIEND . $intHexId . '/hoat-dong.html';
		}
	}
	public function linkFullhouseForPhoto($strUsername, $intHexId) {		
		if($strUsername != '') {
			return HOST_VFRIEND . $strUsername . '/hinh-anh.html';
		} else {
			return HOST_VFRIEND . $intHexId . '/hinh-anh.html';
		}
	}
	public function linkFullhouseForAlbum($strUsername, $intHexId, $intId, $strName) {
		$strName = str_replace('/', ' ', $strName);
		$strName = Vfriend_Function::convertNoVn($strName);
		if($strUsername != '') {
			return HOST_VFRIEND . $strUsername . '/' . $strName . '-ia' . $intId . '.html';
		} else {
			return HOST_VFRIEND . $intHexId . '/' . $strName . '-ia' . $intId . '.html';
		}
	}
	public function linkFullhouseForPhotoDetail($strUsername, $intHexId, $intId, $strName) {
		$strName = str_replace('/', ' ', $strName);
		$strName = Vfriend_Function::convertNoVn($strName);
		if($strUsername != '') {
			return HOST_VFRIEND . $strUsername . '/' . $strName . '-ipd' . $intId . '.html';
		} else {
			return HOST_VFRIEND . $intHexId . '/' . $strName . '-ipd' . $intId . '.html';
		}
	}
	public function linkBlog($intBlogId, $strTitle)
	{
		$strTitle = str_replace('/', ' ', $strTitle);
		return HOST_VFRIEND  . 'nhat-ky/chi-tiet/'. $intBlogId . '/' . Vfriend_Function::convertNoVn($strTitle) . '.html';
	}
	public function linkBlogForUsername($intHexId, $strTitle)
	{
		$strTitle = str_replace('/', ' ', $strTitle);
		return HOST_BLOG . $intHexId . '/' . Vfriend_Function::convertNoVn($strTitle) . '.html';
	}
	public function linkNews($intNewsId, $strTitle)
	{
		$strTitle = str_replace('/', ' ', $strTitle);
		return HOST_VFRIEND  . 'diem-tin/chi-tiet/'. $intNewsId . '/' . Vfriend_Function::convertNoVn($strTitle) . '.html';
	}
	public function linkListNews($intCateId, $intSubCateId, $strName, $strSubName)
	{
		$strName = str_replace('/', ' ', $strName);
		return HOST_VFRIEND  . 'diem-tin/'. Vfriend_Function::convertNoVn($strName) .'/'.Vfriend_Function::convertNoVn($strSubName) . '.html?cat=' . $intCateId . '&amp;sub=' . $intSubCateId;
	}
	public function linkEditNews($intNewsId, $strName)
	{
		$strName = str_replace('/', ' ', $strName);
		return HOST_VFRIEND  . 'diem-tin/chinh-sua/'.  $intNewsId.'/'.Vfriend_Function::convertNoVn($strName) . '.html';
	}
	public function linkPlaces($intPlaceId, $strTitle)
	{
		$strTitle = str_replace('/', ' ', $strTitle);
		return HOST_VFRIEND  . 'an-choi/chi-tiet/'. $intPlaceId . '/' . Vfriend_Function::convertNoVn($strTitle) . '.html';
	}
	public function linkListPlaces($intCateId, $intSubCateId, $strName, $strSubName)
	{
		$strName = str_replace('/', ' ', $strName);
		return HOST_VFRIEND  . 'an-choi/'. Vfriend_Function::convertNoVn($strName) .'/'.Vfriend_Function::convertNoVn($strSubName) . '.html?cat=' . $intCateId . '&sub=' . $intSubCateId;
	}
	public function linkEditPlaces($intPlaceId, $strName)
	{
		$strName = str_replace('/', ' ', $strName);
		return HOST_VFRIEND  . 'an-choi/chinh-sua/'.  $intPlaceId.'/'.Vfriend_Function::convertNoVn($strName) . '.html';
	}
	public function linkOfflineForCategory($intId, $strName) {
		$strName = str_replace('/', ' ', $strName);
		return HOST_VFRIEND . 'offline/off-' . Vfriend_Function::convertNoVn($strName) .'-tag' . $intId . '.html';
	}
	public function linkOfflineForDetail($intId, $strName) {
		$strName = str_replace('/', ' ', $strName);
		return HOST_VFRIEND . 'offline/' . Vfriend_Function::convertNoVn($strName) .'-off' . $intId . '.html';
	}
}
?>
